package util

import (
	"bytes"
	"encoding/gob"
	json2 "encoding/json"
	"fmt"
	"github.com/vmihailenco/msgpack"
)

func TestJson() {
	var data = make(map[string]interface{})

	data["count"] = 1
	data["name"] = "aa"

	var s1 = make(map[string]interface{})
	s1["data"] = data

	bytes, err := json2.Marshal(s1)
	if err != nil {
		return
	}

	str := string(bytes)
	fmt.Println(str)

	var s2 = make(map[string]interface{})
	err = json2.Unmarshal(bytes, &s2)
	if err != nil {
		return
	}

	fmt.Println(s2)

}

func TestGob() {
	gob.Register(map[string]interface{}{})

	var data = make(map[string]interface{}, 8)
	data["count"] = -1
	var s1 = make(map[string]interface{})
	s1["data"] = data

	buf := new(bytes.Buffer)
	enc := gob.NewEncoder(buf)
	err := enc.Encode(s1)
	if err != nil {
		panic(err)
	}

	var s2 = make(map[string]interface{})
	decoder := gob.NewDecoder(buf)
	err = decoder.Decode(&s2)

	if err != nil {
		panic(err)
	}

	fmt.Println(s2)

}

type Person struct {
	Name   string
	Age    int
	Gender string
}

func TestMsgPack() {
	p1 := Person{
		"aa", 18, "male",
	}

	b, err := msgpack.Marshal(p1)
	if err != nil {
		return
	}
	fmt.Println(b)

	var p2 Person
	err = msgpack.Unmarshal(b, &p2)
	if err != nil {
		return
	}
	fmt.Println(p2)

}
